package com.stox.core.dao.mapper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.stox.core.manager.ScripManager;
import com.stox.core.model.Bar;
import com.stox.core.model.Scrip;
import com.stox.core.util.StringUtil;

public class BarRowMapper implements RowMapper<Bar> {

	private ScripManager scripManager;
	
	public static String getSelectSql(String scripCode){
		return "select code, bardate, open, high, low, close, volume, last, previousClose, turnover, trade from "+getTableName(scripCode)+" where code="+scripCode;
	}
	
	public static String getInsertSql(String scripCode){
		return "insert into "+getTableName(scripCode)+"(code, bardate, open, high, low, close, volume, last, previousClose, turnover, trade) values(?,?,?,?,?,?,?,?,?,?,?)";
	}
	
	public static String getCreateSql(String scripCode){
		return "create table "+getTableName(scripCode)+"(" +
				"code varchar(20) not null," +
				"bardate date not null," +
				"open double not null," +
				"high double not null," +
				"low double not null," +
				"close double not null," +
				"volume double not null," +
				"last double," +
				"previousClose double," +
				"turnover double," +
				"trade double," +
				"primary key(code, bardate))";
	}
	
	public static String getCreateIndexSql(String scripCode){
		String tableName = getTableName(scripCode);
		//return "create unique index "+tableName+"_index on "+tableName+"(bardate)";
		return "create unique index bar_index on "+tableName+"(code,bardate)";
	}
	
	private static String getTableName(String scripCode){
		//return "bar_"+scripCode.trim();
		return "bar";
	}
	
	@Override
	public List<Bar> map(ResultSet rs) throws SQLException {
		List<Bar> barList = new ArrayList<Bar>();
		while(rs.next()){
			Bar bar = new Bar();
			bar.setScripCode(rs.getString("code"));
			bar.setDate(rs.getDate("bardate"));
			bar.setOpen(rs.getDouble("open"));
			bar.setHigh(rs.getDouble("high"));
			bar.setLow(rs.getDouble("low"));
			bar.setClose(rs.getDouble("close"));
			bar.setVolume(rs.getDouble("volume"));
			bar.setLast(rs.getDouble("last"));
			bar.setPreviousClose(rs.getDouble("previousClose"));
			bar.setTurnover(rs.getDouble("turnover"));
			bar.setTrades(rs.getDouble("trade"));
			barList.add(bar);
		}
		return barList;
	}
	
	public void map(PreparedStatement ps, Bar bar) throws SQLException{
		ps.setString(1, bar.getScripCode());
		ps.setDate(2, new java.sql.Date(bar.getDate().getTime()));
		ps.setDouble(3,bar.getOpen());
		ps.setDouble(4, bar.getHigh());
		ps.setDouble(5, bar.getLow());
		ps.setDouble(6, bar.getClose());
		ps.setDouble(7, bar.getVolume());
		ps.setDouble(8, bar.getLast());
		ps.setDouble(9, bar.getPreviousClose());
		ps.setDouble(10, bar.getTurnover());
		ps.setDouble(11, bar.getTrades());
	}

	public Bar parseString(String str){
		Bar bar = new Bar();
		Scrip scrip = null;
		
		if(StringUtil.hasText(str)){
			StringTokenizer t = new StringTokenizer(str,",",false);
			try{
				String scripCode = t.nextToken();
				scrip = scripManager.findByCode(scripCode);
				bar.setScrip(scrip);

				t.nextToken();//scrip.setName(t.nextToken());//bar.setName(t.nextToken());
				t.nextToken();//scrip.setGroup(t.nextToken().trim().toUpperCase());
				t.nextToken();//scrip.setType(t.nextToken().trim().toUpperCase());
				
				bar.setScripCode(scrip.getCode());
				bar.setOpen(Double.parseDouble(t.nextToken()));
				bar.setHigh(Double.parseDouble(t.nextToken()));
				bar.setLow(Double.parseDouble(t.nextToken()));
				bar.setClose(Double.parseDouble(t.nextToken()));
				bar.setLast(Double.parseDouble(t.nextToken()));
				bar.setPreviousClose(Double.parseDouble(t.nextToken()));
				bar.setTrades(Double.parseDouble(t.nextToken()));
				bar.setVolume(Double.parseDouble(t.nextToken()));
				bar.setTurnover(Double.parseDouble(t.nextToken()));
			}catch(NumberFormatException nfe){
				bar = null;
			}catch(Exception e){
				e.printStackTrace();
				bar = null;
			}
		}
		
		return bar;
	}

	public ScripManager getScripManager() {
		return scripManager;
	}

	public void setScripManager(ScripManager scripManager) {
		this.scripManager = scripManager;
	}
	
}
